#=========================================================
# @Author: LANSHANQUAN
# @Date: 2022-01-09
# @Modify: 2023-11-30
# @Function: Using Jenkins to run Java project!
#=========================================================

#!/bin/bash

#JDK所在路径
JAVA_HOME="/usr/local/java/jdk1.8.0_241"

APP_NAME=$1

BASE_PATH=/cloud/myapps/v1

#Java程序所在的目录(classes的上一级目录)
APP_HOME=${BASE_PATH}/${APP_NAME}

#需要启动的Java主程序(main方法类) (需要执行的代码需要打成jar包放到和其他lib同目录下)需要制定全类名

APP_MAINCLASS="${APP_NAME}"

APP_MAINCLASS_PKG_PATH="com.tech.${APP_NAME}"

#拼凑完整的classpath参数，包括指定lib目录下所有的jar(需要把当前服务打成jar 和所有的依赖的第三方jar放在和此脚本同级目录的lib目录中)

#目录结构如下
# ----start.sh 当前shell脚本
# ----lib 存放所有jar
# --------- *.jar

CLASSPATH="."

#for jar in ${APP_HOME}/lib/*.jar; do
#    CLASSPATH="$CLASSPATH":"$jar"
#done

for jar in ${BASE_PATH}/lib/*.jar; do
    CLASSPATH="$CLASSPATH":"$jar"
done

JAR_FILE=${BASE_PATH}/${APP_MAINCLASS}.jar

CLASSPATH="$CLASSPATH":"${BASE_PATH}/${APP_MAINCLASS}.jar"

#下面参数应该写 需要执行当前服务的用户名
RUNNING_USER=ituser

#java虚拟机启动参数
#JAVA_OPTS="-ms512m -mx512m -Xmn128m -Djava.awt.headless=true -XX:MaxPermSize=128m"

JAVA_OPTS="-server -Xms1024M -Xmx1024M"

psid=0

BACKUP_PATH="${BASE_PATH}/backup"
DATE=`date +%Y%m%d%H%M%S`
# DATE=`date +%Y%m%d`

BAK_JAR_FILE=bak-${APP_NAME}-${DATE}.jar

#此方法用于判断当前服务是否已经启动 使用java 的 jps 查看当前系统中是否有当前服务的线程号(比如启动了myappmainclass服务 使用jps 命令就可以看到myappmainclass服务的进程号)

function check()
{
    if [[ ! -d ${BASE_PATH} ]];then
        # mkdir ${APP_HOME}
        echo "[ERROR] ${BATH_PATH} 不存在，无法启动Java程序！"
        exit 1
    fi
    
    if [[ ! -f ${JAR_FILE} ]];then
        echo "[ERROR] ${JAR_FILE} 不存在，无法启动Java程序！"
        exit 1
    fi
}

function checkpid()
{
    javaps=`$JAVA_HOME/bin/jps -l | grep $APP_MAINCLASS`

    if [[ -n "$javaps" ]]; then
        psid=`echo $javaps | awk '{printf $1}'`
    else
        psid=0
    fi
}

function backup()
{
    if [[ ! -d ${BACKUP_PATH} ]];then
        mkdir ${BACKUP_PATH}
    fi

    if [ -f "${JAR_FILE}" ]; then
        echo  "[INFO] 备份 ${JAR_FILE}..."
            cp ${JAR_FILE} ${BACKUP_PATH}/${BAK_JAR_FILE}
        echo  "[INFO] 备份当前的 ${JAR_FILE} 完成"

    else
        echo  "[INFO] 当前的${JAR_FILE}不存在，跳过备份"
    fi
}

function start()
{
    checkpid

    if [ $psid -ne 0 ]; then
        echo "================================"
        echo "warn: $APP_MAINCLASS already started! (pid=$psid)"
        echo "================================"

    else
        echo "[INFO] Starting $APP_MAINCLASS ..."

        # JAVA_CMD="nohup java $JAVA_OPTS -classpath $CLASSPATH $APP_MAINCLASS >/dev/null 2>&1 &"

        # nohup java $JAVA_OPTS -classpath $CLASSPATH $APP_MAINCLASS > ./nohup.out 2>&1 &

        #nohup java $JAVA_OPTS -classpath $CLASSPATH $APP_MAINCLASS > /dev/null 2>&1 &
        nohup java $JAVA_OPTS -classpath $CLASSPATH $APP_MAINCLASS_PKG_PATH > /dev/null 2>&1 &

        # su - $RUNNING_USER -c "$JAVA_CMD"

        checkpid

        if [[ $psid -ne 0 ]]; then
            echo "[INFO] Start $APP_MAINCLASS success! (pid=$psid)"
        else
            echo "[ERROR] Start $APP_MAINCLASS failed!"
            exit 1
        fi
    fi
}

function stop()
{
    checkpid
    if [[ $psid -ne 0 ]]; then
        echo "[INFO] Stopping $APP_MAINCLASS ...(pid=$psid) "
        kill -9 $psid

        # su - $RUNNING_USER -c "kill -9 $psid"
    else
        echo "[INFO] The app has already stopped !"
    fi

    if [[ $? -eq 0 ]]; then
        echo "[INFO] Stop $APP_MAINCLASS success!"
    else
        echo "[ERROR] Stop $APP_MAINCLASS failed!"
        exit 1

    fi
}

function fn_main()
{
    if [[ $# -ne 2 ]];
    then
        echo "[ERROR] Parameter error, eg: bash app.sh TServer start!"
        exit 1
    fi

    if [[ "$2"x != "stop"x && "$2"x != "start"x && "$2"x != "restart"x && "$2"x != "deploy"x ]];then
        echo "[ERROR] Parameter error, eg: bash app.sh TServer start!"
        exit 1
    fi
    
    check
    
    if [[ "$2"x == "stop"x || "$2"x == "STOP"x ]];then
        echo "[INFO] Stop app now!"
        stop
        return 0
    fi

    if [[ "$2"x == "start"x || "$2"x == "START"x ]];then
        echo "[INFO] Start app now!"
        start
        return 0
    fi
    
    if [[ "$2"x == "restart"x || "$2"x == "RESTART"x ]];then
        echo "[INFO] Restart app now!"
        stop
        start
        return 0
    fi
    
    if [[ "$2"x == "deploy"x || "$2"x == "deploy"x ]];then
        echo "[INFO] Delpoy app now!"
        stop
        start
        backup
        return 0
    fi
    
}

fn_main "$@"
result=$?

if [[ "$result" -eq 0 ]]
then
    echo "SUCCESS"
else
    echo "FAILED"
fi
